<?php

// $Id$

global $WSC;


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_dbc.class.php');
require_once('i_menu_item_instance.class.php');


class TCMS_Menu_Item_Normal_DBC extends TCMS_Menu_Item_DBC implements Menu_Item_Instance {
	private $url_value = '';

	public function load_by_id(DB_Connection $dbc, $id) {
		$ret = parent::load_by_id($dbc, $id);
		if ($ret) {
			$sql = "select url_value from tcms_menu_items_normal where menu_item = $id";
			$rez = $dbc->execute($sql);
			if (!$rez->is_eod()) {
				$rez_obj = $rez->fetch_object();
				$this->set_url_value($rez_obj->URL_VALUE);
			}
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$id = parent::insert($dbc);
		$sql = "insert into tcms_menu_items_normal (menu_item, url_value) values ($id, '{$this->get_url_value()}') ";
		$dbc->execute($sql);

		return $id;
	}


	protected function update(DB_Connection $dbc) {
		$id = parent::update($dbc);
		$this->delete_existing($dbc, $this->get_id());
		$sql = "insert into tcms_menu_items_normal (menu_item, url_value) values ($id, '{$this->get_url_value()}') ";
		$dbc->execute($sql);

		return $id;
	}


	public function get_url_value() {
		return $this->url_value;

		return $ret;
	}


	public function set_url_value($url_value) {
		$this->url_value = $url_value;
	}


	public function can_publish() {
		return true;
	}


	public static function exists(DB_Connection $dbc, $item_id, $url_value) {
		tangra_if_not_int_throw_e($item_id);
		$url_value = addslashes($url_value);

		$sql = "select menu_item from tcms_menu_items_normal where menu_item = $item_id and url_value = '$url_value'";
		$rez = $dbc->execute($sql);

		return !$rez->is_eod();
	}


	private function delete_existing($dbc, $item_id) {
		$sql = "delete from tcms_menu_items_normal where menu_item = $item_id";

		$rez = $dbc->execute($sql);
	}


	public static function delete(DB_Connection $dbc, $item_id) {
		tangra_if_not_int_throw_e($item_id);

		$sql = "delete from tcms_menu_items_normal where menu_item = $item_id";
		$rez = $dbc->execute($sql);

		TCMS_Menu_Item_DBC::delete($dbc, $item_id);
	}


	public function get_url(DB_Connection $dbc) {
		return $this->get_url_value();

		return $ret;
	}
}